package images;

import com.sun.image.codec.jpeg.JPEGEncodeParam;
import sun.awt.image.ImageDecoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: vladimir
 * Date: 05.08.11
 * Time: 18:55
 * To change this template use File | Settings | File Templates.
 */
public class PNGJPGConverter {
    public static void main(String[] args) throws IOException {
//        File sourceFolder = new File("C:\\Documents and Settings\\vladimir\\Мои документы\\Мои рисунки\\fgarden-temp");
//        File sourceFolder = new File("C:\\backup\\fgarden\\www2\\resource\\images");
//        File sourceFolder = new File("C:\\backup\\fgarden\\www2\\resource\\interesting");
        File sourceFolder = new File("C:\\Users\\Vladimir\\Documents\\Мои рисунки\\extramobi\\apple accs\\");
        new File(sourceFolder.getAbsolutePath() + "\\jpg").mkdir();
        File[] images = sourceFolder.listFiles();
        for (File imageFile : images) {
            if (imageFile.isFile() && imageFile.getName().endsWith(".png")) {
                BufferedImage image = ImageIO.read(imageFile);
                image = image.getSubimage(8, 8, image.getWidth() - 8, image.getHeight() - 8);
                BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
//                Graphics2D g = newImage.createGraphics();
//                g.setComposite(AlphaComposite.Src);
//                g.drawImage(image, 0, 0, newWidth, newHeight, null);
//                g.dispose();
                newImage.createGraphics().drawImage(image, 0, 0, null);
                ImageIO.write(newImage, "jpg", new File(sourceFolder.getAbsolutePath() + "\\jpg\\" + imageFile.getName().replace(".png", ".jpg")));
            }
        }
    }
}
